﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "UiViewMouseEventHandler.h"

#include "IUiViewMouseEventArgs.h"
#include "IUiViewMouseEvent.h"
#include "Vector3d.h"
#include "IMainWindow.h"
#include "IStatusBar.h"
#include "IStatusBarPanel.h"
#include "IControl.h"
#include "ILabelDefinition.h"
#include "IUiView.h"
#include "CommandIds.h"
#include "IActionManager.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace Sample;
using namespace gcmp;

UiViewMouseEventHandler::UiViewMouseEventHandler()
{

}

UiViewMouseEventHandler::~UiViewMouseEventHandler()
{

}

void UiViewMouseEventHandler::On(gcmp::IUiViewMouseEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"UI视图鼠标事件参数不能为空", L"GDMP", L"2023-09-30");

    if (pArgs->GetType() == UiViewMouseEventType::Move)
    {
        std::wstring composedPromptMessages;
        std::vector<std::wstring> promptMessages = IActionManager::GetPromptMessages();
        FOR_EACH(promptMessage, promptMessages)
        {
            if (!promptMessage.empty())
            {
                if (!composedPromptMessages.empty())
                    composedPromptMessages += L" | ";
                composedPromptMessages += promptMessage;
            }
        }

        //设置状态栏提示信息
        SetPromptOfMousePosition(pArgs->GetMousePosition(),composedPromptMessages);

        const gcmp::IUiViewMouseEvent* pMouseMoveEvent = quick_cast<gcmp::IUiViewMouseEvent>(pArgs->GetEvent());
        if (nullptr == pMouseMoveEvent)
        {
            return;
        }

        gcmp::IUiView* pUiView = pMouseMoveEvent->GetSender();
        std::wstring tag = pUiView->GetTag();
        tag = L"";
    }
    else if (pArgs->GetType() == gcmp::UiViewMouseEventType::Hover)
    {
        ElementId elementId = ElementId::InvalidID;
        std::wstring *pTooltip = nullptr;
        pArgs->GetHoverTooltip(elementId, pTooltip);
        if (pTooltip)
        {
            //TODO:自定义提示信息
        }
    }
}

void UiViewMouseEventHandler::SetPromptOfMousePosition(const gcmp::Vector3d pos,const std::wstring& promptMessage)
{
    IMainWindow* pMainWnd = IMainWindow::GetMainWindow();
    if (nullptr == pMainWnd)
    {
        return;
    }

    IStatusBar* pStatusBar = pMainWnd->GetStatusBar();
    if (nullptr == pStatusBar)
    {
        return;
    }

    IStatusBarPanel* pStatusPanel = pStatusBar->GetPanel(STATUSBAR_MOUSE_POSITON);
    if (nullptr == pStatusPanel)
    {
        return;
    }

    IControl* pControl = pStatusPanel->GetControl(STATUSBAR_MOUSE_POSITON);
    if (nullptr == pControl)
    {
        return;
    }

    ILabelDefinition* pLabelDefine = dynamic_cast<ILabelDefinition*>(pControl->GetControlDefinition());
    if (nullptr == pLabelDefine)
    {
        return;
    }
    if (promptMessage.empty())
    {
        pLabelDefine->SetCaption(L"MouseMove（" + gcmp::StringUtil::ToWString(pos.GetX(), 3) + L"," + gcmp::StringUtil::ToWString(pos.GetY(), 3) + L"," + gcmp::StringUtil::ToWString(pos.GetZ(), 3) + L"）");
    }
    else
    {
        const int precision = 2;
        std::wstring message = L"(X=" + StringUtil::ToWString(pos.X(), precision)
            + L", Y=" + StringUtil::ToWString(pos.Y(), precision)
            + L", Z=" + StringUtil::ToWString(pos.Z(), precision) + L")";
        pLabelDefine->SetCaption(message+L" | "+promptMessage);
    }
}


